﻿<%@ Page Language="C#" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="TarreOA.TarreOABLL" %>
<%@ Import Namespace="TarreOA.TarreOAModels  " %>

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.DataList_DataTable();
        } 
    }

    protected void DataList_DataTable()
    {
        User curLoginUser = (User)Session["user"];
        UserCommunicationGrouptbService ucgs = new UserCommunicationGrouptbService();
        List<UserCommunicationGrouptb> list = ucgs.findByUserLoginName(curLoginUser.LoginName);
        if (list != null && list.Count > 0)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("ComGroupName", typeof(string));
            dt.Columns.Add("ComGroupId", typeof(string));
            foreach (UserCommunicationGrouptb userCommunicationGrouptb in list)
            {
                DataRow dr = dt.NewRow();
                dr["ComGroupName"] = userCommunicationGrouptb.ComGroupName;
                dr["ComGroupId"] = userCommunicationGrouptb.ComGroupId.ToString();
                dt.Rows.Add(dr);
            }
            ds.Tables.Add(dt);
            this.DataList1.DataSource = ds.Tables[0].DefaultView;
            this.DataList1.DataBind();
        }
    }

    protected void BtnCreateGroup_Click(object sender, EventArgs e)
    {
        Response.Redirect("CreateComGroup.aspx");
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        if (((Button)sender).Attributes["cgid"] != null && !"".Equals(((Button)sender).Attributes["cgid"]))
        {
            int cgid = int.Parse(((Button)sender).Attributes["cgid"]);
            UserCommunicationGrouptbService ucgs = new UserCommunicationGrouptbService();
            ucgs.delComGroupInfoById(cgid);
            this.DataList_DataTable();
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "window.parent.leftFrame.location.reload();", true);  
        } 
    }

    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            ((Button)e.Item.FindControl("Button4")).Attributes.Add("onclick", "return confirm('您确定删除当前分组吗？')"); 
            string cgid = ((HtmlInputHidden)e.Item.FindControl("hidComGroupId")).Value;
            string cgName = ((Label)e.Item.FindControl("LblComGroupName")).Text; 
            ((Button)e.Item.FindControl("Button4")).Attributes.Add("cgid", cgid);
            ((Button)e.Item.FindControl("Button3")).Attributes.Add("cgid", cgid);
            ((Button)e.Item.FindControl("Button1")).Attributes.Add("cgid", cgid);
            ((Button)e.Item.FindControl("Button1")).Attributes.Add("cgName", cgName);     
        }  
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        string cgid = ((Button)sender).Attributes["cgid"];
        Response.Redirect("UpdateComGroup.aspx?cgid=" + cgid); 
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (((Button)sender).Attributes["cgid"] != null)
        {
            string cgName = ((Button)sender).Attributes["cgName"];
            int cgid = int.Parse(((Button)sender).Attributes["cgid"]);
            UserCommunicationGroupLinkMantbService ucgLinkMantbService = new UserCommunicationGroupLinkMantbService();
            List<UserCommunicationGroupLinkMantb> list = ucgLinkMantbService.findByComGroupId(cgid);
            if (list != null && list.Count > 0)
            {
                StringBuilder sbu = new StringBuilder();
                sbu.AppendLine("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">");
                sbu.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
                sbu.AppendLine("<td>姓名</td>");
                sbu.AppendLine("<td>性别</td>");
                sbu.AppendLine("<td>生日</td>");
                sbu.AppendLine("<td>昵称</td>");
                sbu.AppendLine("<td>职务</td>");
                sbu.AppendLine("<td>单位名称</td>");
                sbu.AppendLine("<td>单位地址</td>");
                sbu.AppendLine("<td>单位电话</td>");
                sbu.AppendLine("<td>单位传真</td>");
                sbu.AppendLine("<td>家庭住址</td>");
                sbu.AppendLine("<td>家庭电话</td>");
                sbu.AppendLine("<td>手机号码</td>");
                sbu.AppendLine("<td>电子邮件</td>");
                sbu.AppendLine("<td>OICQ号码</td>");
                sbu.AppendLine("<td>MSN号码</td>");
                sbu.AppendLine("<td>备注</td>");
                sbu.AppendLine("</tr>");
                foreach (UserCommunicationGroupLinkMantb ucgLinkMan in list)
                {
                    sbu.AppendLine("<tr>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManName + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + (ucgLinkMan.GroupLinkManSec == 0 ? "男" : "女") + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManBirthday.ToShortDateString() + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManName2 + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManDuty + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManUnitName + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManUnitAddress + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManUnitTelephone + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManUnitFacsimile + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManHomeAddress + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManHomeTelephone + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManHandset + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManEmail + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManOicq + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManMsn + "</td>");
                    sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManRemark + "</td>");  
                    sbu.AppendLine("</tr>"); 
                }
                sbu.AppendLine("</table>");
                Page.Response.Clear();
                Page.Response.Buffer = true;
                Page.Response.Charset = "GB2312";
                Page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(cgName, Encoding.UTF8) + ".xls");
                Page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
                Page.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
                Page.EnableViewState = false;
                Page.Response.Write(sbu.ToString());
                Page.Response.End();

            }   
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>

    <script type="text/javascript" src="../../js/UserCommunication.js"></script>
     <link rel="Stylesheet" type="text/css" href="../../Styles/right_style.css" />
    <script type="text/javascript" src="../../Scripts/jquery.ui/js/jquery.js"></script>


    <style type="text/css">
<!--
html{ scrollbar-base-color: #dbecf8; scrollbar-arrow-color: #2a8ed1; scrollbar-track-color: #bdddf2; scrollbar-3dlight-color: #2886c5; scrollbar-darkshadow-color: #2886c5;
scrollbar-face-color: #dbecf8; scrollbar-shadow-color: #dbecf8;  }
-->
</style>

</head>
<body>
    <form id="form1" runat="server">
        <div class="sub_right_top">
            <div class="add_book_right">
      <div class="add_book_new" align="left"><span class="add_book_newword">新建分组</span><span class="add_book_newbut">
      <asp:Button ID="BtnCreateGroup" runat="server" Text="新建分组" Width="125px" OnClick="BtnCreateGroup_Click" />
      </span></div>
      <div class="add_book_guanli" align="left">管理分组</div>

        <asp:Repeater ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
                        <HeaderTemplate>
                            <table width="97%" border="1" cellpadding="0" cellspacing="0" bordercolor="#72b0d8" style="border-collapse:collapse; ">
                               <tr bgcolor="#b1d5ef">
                                <td align="center">分组名称</td>

                                <td align="center">操作</td>
                              </tr>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr bgcolor="white">
                                <td align="center">
                                    <asp:Label ID="LblComGroupName" runat="server"  Text='<%# Eval("ComGroupName") %>' ></asp:Label>
                                            <input id="hidComGroupId" type="hidden" runat="server" value='<%# Eval("ComGroupId") %>' />
                                </td>
                                <td align="center">
                                    <asp:Button ID="Button3" runat="server" Text="编辑" Width="67px" OnClick="Button3_Click" />
                                        <asp:Button ID="Button4" runat="server" Height="24px" Text="删除" Width="67px" OnClick="Button4_Click" />
                                        <asp:Button ID="Button1" runat="server" Text="导出Excel文件" OnClick="Button1_Click" />
                            <input id="HidComGroupName" runat="server" style="width: 55px" type="hidden" />
                                </td>
                            </tr> 
                        </ItemTemplate>
                        <FooterTemplate>
                            </table>
                        </FooterTemplate>
           </asp:Repeater>
        </div>
        </div>
    </form>
</body>
</html>
